﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies
{
    public partial class Agenda : UserControl
    {
        private MySqlDataReader reader2;
        private String currentDate;
        private int projectID;
        private Boolean error;
        
        public Agenda()
        {

        }

        public Agenda(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            getAgenda();
            comboBox1.Hide();
            richTextBox3.Hide();
            button4.Hide();
            label5.Hide();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentDate = (String)listBox1.SelectedItem;
            getNote(currentDate);
        }

        public void getAgenda()
        {
            try
            {
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand agendaGet = new MySqlCommand("SELECT agendaID, date FROM agenda WHERE projectID=@projectID", grabConnection);
                agendaGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                grabConnection.Open();
                MySqlDataReader reader = agendaGet.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        grabConnection.Close();
                        reader.Dispose();
                        agendaGet.Dispose();
                    }
                    else
                    {
                        while (reader.Read())
                        {
                            int nummer = reader.GetInt32(0);
                            String date = reader.GetString(1);
                            listBox1.Items.Add(date);
                        }
                    }

                    grabConnection.Close();
                    reader.Dispose();
                    agendaGet.Dispose();
                }
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void getNote(String currentDate)
        {
            try
            {
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand noteGet = new MySqlCommand("SELECT note FROM agenda WHERE projectID=@projectID AND date=@currentDate", grabConnection);
                noteGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                noteGet.Parameters.Add(new MySqlParameter("currentDate", currentDate));
                grabConnection.Open();
                reader2 = noteGet.ExecuteReader();

                if (reader2.Read() != false)
                {
                    if (reader2.IsDBNull(0) == true)
                    {
                        grabConnection.Close();
                        reader2.Dispose();
                        noteGet.Dispose();
                    }

                    else
                    {
                        string note = reader2.GetString(0);
                        richTextBox2.Text = note;
                        grabConnection.Close();
                        reader2.Dispose();
                        noteGet.Dispose();
                    }
                }
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Controls.Clear();
            Controls.Add(new Functies.Agendapunten(projectID));
        }

        public void editNote(string date, int projectID)
        {
            string changeDate = date;
            int changeProject = projectID;
            string newNote = richTextBox3.Text;
            if (changeDate != "")
            {
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand agenda = new MySqlCommand("UPDATE agenda SET note=@note WHERE date=@date", grabConnection);
                agenda.Parameters.Add(new MySqlParameter("date", changeDate));
                agenda.Parameters.Add(new MySqlParameter("note", newNote));

                try
                {
                    grabConnection.Open();
                    agenda.ExecuteNonQuery();
                }
                catch (MySqlException)
                {
                    error = true;
                }
                finally
                {
                    grabConnection.Close();
                    if (error == true)
                    {
                        MessageBox.Show("sql error detected!");
                    }
                    else
                    {
                        MessageBox.Show("Edit completed.");
                    }
                }
            }
            else
            {
                MessageBox.Show("Geen nieuwe note ingesteld");
            }
        }

        public void button3_Click(object sender, EventArgs e)
        {
            richTextBox2.Hide();
            richTextBox3.Show();
            label4.Hide();
            label5.Show();
            button3.Hide();
            button4.Show();
            richTextBox3.Text = richTextBox2.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            editNote(currentDate, projectID);
            richTextBox3.Hide();
            richTextBox2.Show();
            button4.Hide();
            button3.Show();
        }

        private void richTextBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
